// pages/category/category.js

var httpData = require("data.js");

var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    preSelectedCategoryId: -1,//从其它页面跳转过来时想要选中的分类ID,-1表示.
    currentIndex: 0,//当前选中项索引.
    categories: [],//分类数据.
    commodities: [],//商品数据.
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("生命周期函数--onLoad--监听页面加载");
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    console.log("生命周期函数--onReady--监听页面初次渲染完成");
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log("生命周期函数--onShow--监听页面显示");

    if (this.data.categories.length == 0) {//获取分类数据.
      this.getCategoriesData();
    } else if (app.globalData.category.preSelectedCategoryId > 0) {//需要处理切换选中项.
      this.handlePendingSelectedEvent();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    console.log("生命周期函数--onHide--监听页面隐藏");
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 获取分类数据
   */
  getCategoriesData: function () {
    let categories = httpData.getCategoriesData();
    this.setData({
      categories: categories
    });

    if (app.globalData.category.preSelectedCategoryId > 0) {
      handlePendingSelectedEvent();
    } else { //选则分类,默认选则第一个.
      this.selectedCategory(0);
    }
  },

  /**
   * 处理可能从其它界面跳转来的选中项切换.
   */
  handlePendingSelectedEvent: function () {
    let preSelectedCategoryId = app.globalData.category.preSelectedCategoryId;
    if (!preSelectedCategoryId || preSelectedCategoryId < 0) {
      return;
    }

    app.globalData.category.preSelectedCategoryId = -1;

    let index = this.findCategoryIndexByCategoryId(preSelectedCategoryId);
    //选则分类.
    this.selectedCategory(index);
  },

  /**
   * 根据分类ID获取分类在分类列表中的索引.
   * @param {Number} id 分类ID.
   */
  findCategoryIndexByCategoryId: function (id) {
    let categories = this.data.categories;
    let N = categories.length;
    let resultIndx = 0;
    for (let index = 0; index < N; index++) {
      if (categories[index].id == id) {
        return index;
      }
    }
    return resultIndx;
  },

  /**
   * 左侧分类栏item点击事件监听器.
   */
  onLeftSelectChangedListener: function (event) {
    //获取当前点击项索引.
    let index = parseInt(event.currentTarget.dataset.index);
    this.selectedCategory(index);
  },

  /**
   * 选则分类.
   * @param {Number} index 分类索引. 
   */
  selectedCategory: function (index) {
    this.setData({
      currentIndex: index,//设置当前选中项为点击项.
    });
    //更新右侧商品列表数据.
    this.getWithUpdateCommoditiesData();
  },

  /**
   * 更新右侧商品数据.
   */
  getWithUpdateCommoditiesData: function () {
    let category = this.data.categories[this.data.currentIndex];
    let commodities = category.commodities;

    if (commodities) {//从缓存数据获取商品数据.
      this.setData({
        "commodities": commodities
      });
    } else {//获取商品数据.
      let that = this;

      commodities = httpData.getCommodities(category.id);
      //缓存分类商品数据.
      category.commodities = commodities;
      that.setData({
        "commodities": commodities
      });
    }
  },

})